<?php

namespace app\admin\controller;


use app\admin\model\Admin;
use app\admin\model\Substation;

/**
 * Author:Jack Wang
 * Time:2018-10-16 15:33:13
 * Describe:登录控制器
 * Class Login
 * @package app\admin\controller
 */
class Login extends Base
{

    //登录
    public function index(){
        //已登录处理
        $LFF = cookie('LFF');
        if(!empty($LFF) && $LFF !== false){
            $this->redirect('/Admin/Home/index');
        }
        $WL = session("WL");//获取标识
        if(request()->isPost()) {
            if(!empty($WL)) {//登录状态判断
                $Admin = new Admin();
                $data = input("post.");
                $msg = $Admin->checkData($data, [
                    ["user_name", "require", "登录名不能为空"],
                    ["password", "require", "密码不能为空"],
                ]);
                if ($msg === true) {
                    $is_frozen = $Admin->checkName($data['user_name']);//验证帐号是否存在
                    if ($is_frozen != null) {//验证帐号
                        if($is_frozen != 1) {//没有被冻结
                            if ($this->checkLoginError($WL)) {
                                $this->admin = $Admin->checkPassword($data['user_name'], $data['password']);//验证密码
                                if (!empty($this->admin)) {//验证密码
                                    if ($this->admin["is_frozen"] == 0) {
                                        $LFF = $this->authCode($this->admin["admin_id"], "ENCODE", $this->key);
                                        cookie('LFF', $LFF);
                                        $result["code"] = 0;
                                        $result["msg"] = "验证成功";
                                        $result["url"] = url("Home/index");
                                        cookie('loginRecord',json_encode([
                                            "last_time"  => $this->admin["last_time"],
                                            "update_ip"  => $this->admin["update_ip"],
                                        ]));//登录缓存
                                        $Admin->loginRecord($this->admin["admin_id"]);//登录记录
                                        session($WL, null);//清除登录状态
                                        cache($WL, null);//清除登录记录
                                        //验证多网站管理权限
                                        //if($this->admin["group_id"] != config("SuperAdminGroupId")) {//是否为超级管理员
                                            $Substation = new Substation();
                                            $userListCount = $Substation->userListCount($this->admin["group_id"]);
                                            if((int)$userListCount <= 1){//是否有多个网站管理权限
                                                $userList = $Substation->userLoginList($this->admin["group_id"]);//获取管理的网站
                                                $this->substations = $userList[0]["substation_id"];
                                                session("substations",$this->substations);//设置当前网站
                                            }
                                        //}
                                    } else {
                                        $result["code"] = 1;
                                        $result["msg"] = "帐号被冻结，请联系管理员！";
                                    }
                                } else {
                                    $result["code"] = 1;
                                    $result["msg"] = "密码不正确！";
                                    $this->LoginError($WL);
                                }
                            } else {
                                $result["code"] = 1;
                                $result["msg"] = "错误次数过多，帐号冻结！";
                                $Admin->loginFrozen($data['user_name']);//冻结帐号
                                cache($WL,0);//冻结以后重置
                            }
                        }else{
                            $result["code"] = 2;
                            $result["msg"] = "帐号被冻结，请联系管理员！";
                        }
                    } else {
                        $result["code"] = 1;
                        $result["msg"] = "帐号不正确！";
                    }
                } else {
                    $result["code"] = 1;
                    $result["msg"] = $msg;
                }
            }else{
                $result["code"] = 1;
                $result["msg"] = "登录状态错误，刷新页面重试！";
            }
        }else{
            if(empty($WL)) {
                $WL = $this->getWL();//设置标识
                session("WL",$WL);//设置标识
                cache($WL,0);//设置错误次数
            }
        }
        return !empty($result) ? json($result) : view('index',[
            'title'         =>  '别墅工场网站管理系统登录',
        ]);
    }

    //设置标识
    private function getWL(){
        $WL = RandomString(18);
        if(cache($WL) !== false){
            $this->getWL();
        }
        return $WL;
    }

    //验证错误次数是否超出
    private function checkLoginError($WL){
        $is = false;
        $loginCount = cache($WL);
        if($loginCount <= config("LoginCount")){
            $is = true;
        }
        return $is;
    }

    //设置登录错误次数
    private function LoginError($WL){
        $loginCount = cache($WL);
        $loginCount++;
        cache($WL,$loginCount);
        $this->checkLoginError($WL);
    }


    //退出登录
    public function loginOut(){
        cookie('loginRecord'.$this->admin["admin_id"],null);//清除缓存退出
        cookie('LFF',null);//清除缓存退出
        //清除session缓存
        session('admin',null);
        session('substations',null);
        session('after_limit',null);
        session('after_page',null);
        $this->redirect( '/adminLogin');
    }

}
